<?php

namespace App\Http\Controllers\Admin;

use App\Models\Classes;
use App\Models\Lesson;
use App\Models\Teacher;
use App\Models\TeacherLevel;
use App\Models\Type;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

/**
 * 用户分类管理
 * Class TypeController
 * @package App\Http\Controllers\Admin
 */
class ClassesController extends BaseController
{
    /**
     * 分类列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $list = Classes::paginate(config('site.other.paginate'));

        return backendView('classes.index', compact('list'));
    }

    /**
     * 新增分类
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'c_code' => 'required|max:255|unique:classes,c_code',
                'g_code' => 'required|max:255',
                'type' => 'required|in:01,02',
                'name' => 'required|max:255',
            ], [], [
                'c_code' => __("code"),
                'g_code' => __("grade"),
                'type' => __("category"),
                'name' => __("Name"),
            ]);

            $data = $request->only(['name','c_code','g_code','type']);
            //$data['tl_code'] = rand(100000,999999);
            if (Classes::create($data)) {
                return redirect()->route('admin.lesson.index')->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            return backendView('classes.add');
        }
    }

    /**
     * 编辑分类
     * @param Request $request
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function edit(Request $request, $id = null)
    {

        $lesson = Lesson::find($id);
        if (empty($lesson)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        if ($request->isMethod('POST')) {

            $this->validate($request, [
                'name' => ['required', 'max:255', Rule::unique('types')->ignore($lesson->id)],
                'l_code' => 'required|max:255',
            ], [], [
                'name' => '分类名称',
                'l_code' => __("code"),
            ]);

            $data = $request->only(['name','l_code']);

            if ($lesson->update($data)) {
                return redirect()->route('admin.lesson.index')->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'))->withInput();

        } else {
            return backendView('lesson.edit', compact('lesson'));
        }
    }

    /**
     * 查看分类
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function view($id = null)
    {
        /**
         * @var $type Type
         */
        $type = Type::find($id);
        if (empty($type)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        return backendView('type.view', compact('type'));
    }

    /**
     * 删除分类
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        /**
         * @var $type Type
         */
        $lesson = Lesson::find($id);
        if (empty($lesson)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        try {
//            $users = $type->users()->get();
//            $agencies = $type->agencies()->get();
//
//            if (count($users) > 0 || count($agencies) > 0) {
//                return back()->with('error', '已使用的分类不能删除');
//            }

            if ($lesson->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            return back()->with('error', $e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }
}